package com.luo.chengrui.module.sa.service.matrix;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.luo.chengrui.framework.sadatapermission.core.annotation.SaDataPermission;
import com.luo.chengrui.module.sa.controller.admin.matrix.vo.*;
import com.luo.chengrui.module.sa.convert.matrix.OporgManagerDefineConvert;
import com.luo.chengrui.module.sa.dal.dataobject.matrix.OporgManagerDefineDO;
import com.luo.chengrui.module.sa.dal.mysql.matrix.OporgManagerDefineMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.luo.chengrui.module.sa.enums.ErrorCodeConstants.*;

;

/**
 * 组织管理岗定义 Service 实现类
 *
 * @author luodz
 */
@Service
@Validated
public class OporgManagerDefineServiceImpl implements OporgManagerDefineService {

    @Resource
    private OporgManagerDefineMapper oporgManagerDefineMapper;

    @Override
    public String createOporgManagerDefine(OporgManagerDefineCreateReqVO createReqVO) {
        // 插入
        OporgManagerDefineDO oporgManagerDefine = OporgManagerDefineConvert.INSTANCE.convert(createReqVO);

        validateFieldExists(oporgManagerDefine.getId(), OporgManagerDefineDO::getCode, oporgManagerDefine.getCode(), OPORG_MANAGER_DEFINE_CODE_EXISTS);
        validateFieldExists(oporgManagerDefine.getId(), OporgManagerDefineDO::getName, oporgManagerDefine.getName(), OPORG_MANAGER_DEFINE_NAME_EXISTS);
        oporgManagerDefineMapper.insert(oporgManagerDefine);

        // 返回
        return oporgManagerDefine.getId();
    }

    @Override
    public void updateOporgManagerDefine(OporgManagerDefineUpdateReqVO updateReqVO) {
        // 校验存在
        validateOporgManagerDefineExists(updateReqVO.getId());
        validateFieldExists(updateReqVO.getId(), OporgManagerDefineDO::getCode, updateReqVO.getCode(), OPORG_MANAGER_DEFINE_CODE_EXISTS);
        validateFieldExists(updateReqVO.getId(), OporgManagerDefineDO::getName, updateReqVO.getName(), OPORG_MANAGER_DEFINE_NAME_EXISTS);
        // 更新
        OporgManagerDefineDO updateObj = OporgManagerDefineConvert.INSTANCE.convert(updateReqVO);
        oporgManagerDefineMapper.updateById(updateObj);
    }

    @Override
    public void deleteOporgManagerDefine(String id) {
        // 校验存在
        validateOporgManagerDefineExists(id);
        // 删除
        oporgManagerDefineMapper.deleteById(id);
    }

    private void validateOporgManagerDefineExists(String id) {
        if (oporgManagerDefineMapper.selectById(id) == null) {
            throw exception(OPORG_MANAGER_DEFINE_NOT_EXISTS);
        }
    }

    private void validateFieldExists(String id, SFunction<OporgManagerDefineDO, ?> field, Object value, final ErrorCode errorCode) {
        OporgManagerDefineDO entity = oporgManagerDefineMapper.selectOne(field, value);
        if (entity == null) {
            return;
        }
        if (id == null || !Objects.equals(entity.getId(), id)) {
            throw exception(errorCode);
        }
    }

    @Override
    public OporgManagerDefineDO getOporgManagerDefine(String id) {
        return oporgManagerDefineMapper.selectById(id);
    }

    @Override
    public List<OporgManagerDefineDO> getOporgManagerDefineList(Collection<String> ids) {
        return oporgManagerDefineMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<OporgManagerDefineDO> getOporgManagerDefinePage(OporgManagerDefinePageReqVO pageReqVO) {
        return oporgManagerDefineMapper.selectPage(pageReqVO);
    }

    @Override
    public List<OporgManagerDefineDO> getOporgManagerDefineList(OporgManagerDefineExportReqVO exportReqVO) {
        return oporgManagerDefineMapper.selectList(exportReqVO);
    }

    @Override
    @SaDataPermission(permissinCode = "managerDefinePermission")
    public List<OporgManagerDefineDO> getOporgManagerDefineAllList(OporgManagerDefineBaseVO pageVO) {
        return oporgManagerDefineMapper.selectAllList(pageVO);
    }
}
